<#include "macro_.ftl">
package ${packageCode}.${moduleMapperKey}.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
<#list poMapperList as poMapper>
<#if poMapper.markService && poMapper.markController>
import ${packageCode}.${moduleMapperKey}.dto.${poMapper.code}Dto;
</#if>
</#list>
import com.fand.platform.configuration.service.ConfigurationService;
<#list poMapperList as poMapper>
<#if poMapper.markService && poMapper.markController>
import ${packageCode}.${moduleMapperKey}.validator.${poMapper.code}Validator;
</#if>
</#list>
import ${packageCode}.${moduleMapperKey}.vo.${businessCode}Vo;
import ${packageCode}.${moduleMapperKey}.service.${moduleMapperKey?cap_first}Service;
import com.google.common.collect.Maps;

/**
 * ${businessMapper.name!businessCode}
 *
 * @author ${author!'yaoHT'}
 */
@Controller
public class ${businessCode}Controller {
	@Qualifier(${moduleMapperKey?cap_first}Service.BEAN_ID)
	@Autowired(required = false)
	private ${moduleMapperKey?cap_first}Service ${moduleMapperKey}Service;
<#list poMapperList as poMapper>
<#if poMapper.markService && poMapper.markController>

	@Qualifier(${poMapper.code}Validator.BEAN_ID)
	@Autowired(required = false)
	private Validator ${poMapper.code?uncap_first}Validator;
</#if>
</#list>
<#list poMapperList as poMapper>
<#if poMapper.markService && poMapper.markController>

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> admin${poMapper.code}Delete(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.${moduleMapperKey}Service.get${businessCode}Service().delete${poMapper.code}ByIds(${businessCode?uncap_first}Vo.get${poMapper.code}());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/edit.html" }, method = RequestMethod.GET)
	public String admin${poMapper.code}Edit(final ${businessCode}Vo ${businessCode?uncap_first}Vo, final Model model) {
		final ${poMapper.code}Dto ${poMapper.code?uncap_first} = this.${moduleMapperKey}Service.get${businessCode}Service().
				get${poMapper.code}ById(${businessCode?uncap_first}Vo.get${poMapper.code}());

		model.addAttribute("${poMapper.code?uncap_first}", ${poMapper.code?uncap_first});

		return "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/edit";
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}.html", "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/index.html" })
	public String admin${poMapper.code}Index() {
		return "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/index";
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> admin${poMapper.code}IndexPagination(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		final PaginationDto<Map<String, Object>> ${poMapper.code?uncap_first}Pagination = this.${moduleMapperKey}Service.get${businessCode}Service()
				.pagination${poMapper.code}By1ForMapList(${businessCode?uncap_first}Vo.get${poMapper.code}());

		final Map<String, Object> ${poMapper.code?uncap_first}PaginationMap = Maps.newHashMap();

		${poMapper.code?uncap_first}PaginationMap.put("total", ${poMapper.code?uncap_first}Pagination.getTotalSize());
		${poMapper.code?uncap_first}PaginationMap.put("rows", ${poMapper.code?uncap_first}Pagination.getDataList());

		return ${poMapper.code?uncap_first}PaginationMap;
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/new.html" }, method = RequestMethod.GET)
	public String admin${poMapper.code}New(final ${businessCode}Vo ${businessCode?uncap_first}Vo, final Model model) {
		model.addAttribute("${poMapper.code?uncap_first}", ${businessCode?uncap_first}Vo.get${poMapper.code}());

		return "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/new";
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String admin${poMapper.code}Save(final ${businessCode}Vo ${businessCode?uncap_first}Vo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.${poMapper.code?uncap_first}Validator.validate(${businessCode?uncap_first}Vo.get${poMapper.code}(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.${moduleMapperKey}Service.get${businessCode}Service().save${poMapper.code}ByIdOrCodeFor${poMapper.code}(${businessCode?uncap_first}Vo.get${poMapper.code}());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/validationBy1.json" })
	public @ResponseBody
	Boolean admin${poMapper.code}ValidationBy1(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		try {
			final int count = this.${moduleMapperKey}Service.get${businessCode}Service().read${poMapper.code}ByCodeNqIdForInteger(
					${businessCode?uncap_first}Vo.get${poMapper.code}());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/view.html" }, method = RequestMethod.GET)
	public String admin${poMapper.code}View(final ${businessCode}Vo ${businessCode?uncap_first}Vo, final Model model) {
		final ${poMapper.code}Dto ${poMapper.code?uncap_first} = this.${moduleMapperKey}Service.get${businessCode}Service().
					get${poMapper.code}ById(${businessCode?uncap_first}Vo.get${poMapper.code}());

		model.addAttribute("${poMapper.code?uncap_first}", ${poMapper.code?uncap_first});

		return "/admin/${moduleMapperKey}/${businessCode?uncap_first}/${poMapper.code?uncap_first}/view";
	}
</#if>
</#list>
}
